#delimit;
local fileloc = "~/KMS_REPLICATION";
set logtype text;
capture log close zip_level_traffic;

log using `fileloc'/log_files/zip_level_traffic.txt, name(zip_level_traffic) replace;
set more off;
pause on;
clear all;

******************************************************************;
************* CREATING WEIGHTED TRAFFIC - BY ZIP CODE ************;
******************************************************************;

clear;

foreach dist_cut in 5 10 15 20 {;	

	use `fileloc'/data/location_data/traffic_to_zip_distance.dta, clear;

	drop if distance > `dist_cut' | distance <= (`dist_cut'-5) ;
	sort id;
		
	joinby id using `fileloc'/data/traffic_data/dta_files/weekly_traffic.dta;

	gen weight = 1-sqrt(distance/15); 

	foreach var in tot_flow flow_by_length {;
		gen `var'_weight_`dist_cut' = `var' * weight;
		rename `var' `var'_`dist_cut';
	};
			
	collapse (sum) tot_flow* flow_by_length* (count) loops_`dist_cut' = flow_by_length_`dist_cut', by(mother_zip week) fast;		

	format *flow* %16.0f;

	sort mother_zip week;

	sum *;

	save `fileloc'/data/traffic_data/dta_files/`dist_cut'mile_traffic.dta, replace;

};

log close zip_level_traffic;
